package io.kwdb.examples;

import io.kwdb.sdk.*;

/** Test DEADLINE_EXCEEDED error code with server-side delay */
public class TestDeadlineExceeded {
  public static void main(String[] args) {
    System.out.println("Testing DEADLINE_EXCEEDED error code");
    System.out.println("Server has 2000ms injected delay, client timeout is 500ms");
    System.out.println();

    // Configure client with 500ms timeout
    // Server has 2000ms delay, so request will timeout
    Config config =
        Config.builder()
            .host("127.0.0.1")
            .httpPort(9091)
            .transport(Config.Transport.ILP)
            .timeoutMillis(500) // 500ms timeout
            .build();

    try (KwdbClient client = KwdbClient.create(config)) {
      Point point =
          Point.builder("sdk_bench")
              .tag("host", "deadline-test")
              .field("usage", 1.0)
              .timestampNow()
              .build();

      System.out.println("Sending write request (will timeout)...");
      long startTime = System.currentTimeMillis();
      Status status = client.write(point);
      long elapsed = System.currentTimeMillis() - startTime;

      System.out.println("Elapsed time: " + elapsed + "ms");
      System.out.println("Status code: " + status.getCode());
      System.out.println("Status message: " + status.getMessage());

      if (status.getCode() == Status.Code.DEADLINE_EXCEEDED) {
        System.out.println("\n✓ SUCCESS: Got DEADLINE_EXCEEDED as expected");
      } else {
        System.out.println("\n✗ FAILED: Expected DEADLINE_EXCEEDED, got " + status.getCode());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
